package com.hmall.gateway.config;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.rsa.crypto.KeyStoreKeyFactory;

import java.security.KeyPair;

/**
 * 工具类配置
 *
 * @author Zhy
 * @version 1.0
 * @date 2025-03-18 11:26
 */
@Configuration
@EnableConfigurationProperties(JwtProperties.class)
public class SecurityConfig {

	@Bean
	public PasswordEncoder passwordEncoder() {
		return new BCryptPasswordEncoder();
	}

	@Bean
	public KeyPair keyPair(JwtProperties properties) {
		// 获取秘钥工厂
		KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(properties.getLocation(),
			properties.getPassword().toCharArray()
		);
		// 读取钥匙对
		return keyStoreKeyFactory.getKeyPair(properties.getAlias(), properties.getPassword().toCharArray());
	}
}